﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.IO;
using System.Net;

using ScrumTable.Common.Logging;
using ScrumTable.Common.Properties;

#endregion

namespace ScrumTable.Common.Net
{
    /// <summary>
    /// This class is used to provide additional functionality to the WebRequest
    /// class.
    /// </summary>
    public static class WebRequestUtil
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const string RequestImageMethod = "GET";
        private const int RequestTimeout = 5000; // ms

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Loads the binary data from the underlying url.
        /// </summary>
        public static WebResponse GetDataStream(this WebRequest request, ICredentials credentials)
        {
            PreCondition.AssertNotNull(request, "request");

            request.Credentials = credentials;
            request.Method = RequestImageMethod;
            request.Timeout = RequestTimeout;
            return request.GetResponse();
        }

        /// <summary>
        /// Tries to load the string data from the underlying url.
        /// </summary>
        /// <param name="request">Specifies request instance.</param>
        /// <param name="credentials">Specifies the network credentials.</param>
        public static string TryDownloadTextData(this WebRequest request, ICredentials credentials)
        {
            PreCondition.AssertNotNull(request, "request");

            try
            {
                using (WebResponse response = request.GetDataStream(credentials))
                {
                    using (Stream responseData = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseData))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (WebException e)
            {
                Logger.WarnFormat(Resources.ExcWebRequestUtilDownloadFailed, request.RequestUri, e);
            }
            return null;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
